import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, In } from 'typeorm';
import { TB_DP_Mete } from '../entity/Mete.entity';
import { DPModelInfoQueryDto, DPModelInfoTypeQueryDto, DPModelInfoAddDto, DPModelInfoUpdateDto, DPModelInfoDeleteDto, DPModelInfoBatchUpdateDto } from '../dto/Mete.dto';

@Provide()
export class MeteService {
  @InjectEntityModel(TB_DP_Mete)
  private readonly meteRepository: Repository<TB_DP_Mete>;

   // 根据样本类型查询样本信息，并根据 Type 返回对应的字段
   async findByType(query: DPModelInfoTypeQueryDto) {
    try {
      const { Type } = query;
      const where: any = {};

      if (Type) {
        where['Type'] = Type;
      }

      const results = await this.meteRepository.find({
        where,
      });

      let responseData: any[] = [];

      // 根据 Type 返回不同的字段
      if (Type === 1) {
        // Type 1: 返回 Size
        responseData = results.map(item => ({ label: item.Size, value: item.Size }));
      } else if (Type === 2) {
        // Type 2: 返回 Res
        responseData = results.map(item => ({ label: item.Res, value: item.Res }));
      } else if (Type === 3) {
        // Type 3: 返回 SampleType
        responseData = results.map(item => ({ label: item.SampleType, value: item.SampleType }));
      }

      return {
        success: true,
        data: responseData,
        message: '查询成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询失败: ' + error.message,
      };
    }
  }
  // 根据查询条件获取样本信息，支持分页
  async findSamples(query: DPModelInfoQueryDto) {
    const { sampleType, sampleSizes, resolutions, page = 1, limit = 10 } = query;

    const where: any = {};

    if (sampleType) {
      where['SampleType'] = sampleType;
    }

    if (sampleSizes && sampleSizes.length > 0) {
      where['Size'] = In(sampleSizes);
    }

    if (resolutions && resolutions.length > 0) {
      where['Res'] = In(resolutions);
    }

    try {
      const [mete, total] = await this.meteRepository.findAndCount({
        where,
        skip: (page - 1) * limit,  // 跳过的条数
        take: limit,               // 每页返回的条数
      });

      return {
        success: true,
        data: mete,
        total,                      // 返回总条数
        message: '查询样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询样本信息失败: ' + error.message,
      };
    }
  }

  // 新增样本信息
  async addMete(params: DPModelInfoAddDto) {
    try {
      let mete = new TB_DP_Mete();
      mete.SampleType = params.sampleType;
      mete.Size = params.sampleSize;
      mete.Res = params.resolution;
      mete.Type = params.type;

      const result = await this.meteRepository.save(mete);
      return {
        success: true,
        data: result,
        message: '新增样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '新增样本信息失败: ' + error.message,
      };
    }
  }

  // 更新样本信息
  async updateMete(id: number, params: DPModelInfoUpdateDto) {
    try {
      const mete = await this.meteRepository.findOne({
        where: { OBJID: id },
      });

      if (!mete) {
        return {
          success: false,
          message: '样本信息未找到',
        };
      }

      mete.SampleType = params.sampleType || mete.SampleType;
      mete.Size = params.sampleSize || mete.Size;
      mete.Res = params.resolution || mete.Res;
      mete.Type = params.type || mete.Type;

      const result = await this.meteRepository.save(mete);
      return {
        success: true,
        data: result,
        message: '样本信息更新成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '样本信息更新失败: ' + error.message,
      };
    }
  }

  // 删除样本信息
  async deleteMete(params: DPModelInfoDeleteDto) {
    try {
      const { objId } = params;
      const mete = await this.meteRepository.findOne({
        where: { OBJID: objId },
      });

      if (!mete) {
        return {
          success: false,
          message: '要删除的样本信息不存在',
        };
      }

      await this.meteRepository.remove(mete);
      return {
        success: true,
        message: '删除样本信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '删除样本信息失败: ' + error.message,
      };
    }
  }

  // 批量更新样本类型
  async batchUpdateSampleType(params: DPModelInfoBatchUpdateDto) {
    try {
      const { sampleType, sampleIds } = params;
      const mete = await this.meteRepository.find({
        where: {
          OBJID: In(sampleIds),
        },
      });

      for (let sample of mete) {
        sample.SampleType = sampleType;
        await this.meteRepository.save(sample);
      }

      return {
        success: true,
        message: '批量更新样本类型成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '批量更新样本类型失败: ' + error.message,
      };
    }
  }
}
